/* Includes ------------------------------------------------------------------*/
#include <MyApplication.h>

/* Private define-------------------------------------------------------------*/

/* Private variables----------------------------------------------------------*/

/* Public variables-----------------------------------------------------------*/

/* Private function prototypes------------------------------------------------*/

/*
* @name   HAL_GPIO_EXTI_Callback
* @brief  外部中断回调函数
* @param  GPIO_Pin：触发外部中断的引脚
* @retval None   
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  uint8_t i;
  if(GPIO_Pin == KEY1_Pin)
  {
    //步进电机启动或停止
    Uniplar_Step_Motor.Start_Or_Stop();
    //如果是按键按下，则需要发送信息到串口屏，同步页面数据
    if(HMI.Page_Step_Motor_KEY_Flag == FALSE)
    {
      HMI.HMI_SendString((uint8_t*)"click bt0,1");
      HMI.HMI_SendString((uint8_t*)"click bt0,0");
      if(Uniplar_Step_Motor.Status == Start_Status)
      {
        HMI.pucSend_Motor_Cricle_Buffer[7] = Uniplar_Step_Motor.Circle+'0';
        HMI.HMI_SendString(HMI.pucSend_Motor_Cricle_Buffer);     //屏幕显示圈数
        //数码管显示速度
        switch(Uniplar_Step_Motor.Speed)
        {
          case 100: Display.Disp_Hex(Disp_NUM_1,1,Disp_DP_OFF); HMI.ucSpeed_temp = 1;break;
          case 90:  Display.Disp_Hex(Disp_NUM_1,2,Disp_DP_OFF); HMI.ucSpeed_temp = 2;break;
          case 80:  Display.Disp_Hex(Disp_NUM_1,3,Disp_DP_OFF); HMI.ucSpeed_temp = 3;break;
          case 70:  Display.Disp_Hex(Disp_NUM_1,4,Disp_DP_OFF); HMI.ucSpeed_temp = 4;break;
          case 60:  Display.Disp_Hex(Disp_NUM_1,5,Disp_DP_OFF); HMI.ucSpeed_temp = 5;break;
          case 50:  Display.Disp_Hex(Disp_NUM_1,6,Disp_DP_OFF); HMI.ucSpeed_temp = 6;break;
          case 40:  Display.Disp_Hex(Disp_NUM_1,7,Disp_DP_OFF); HMI.ucSpeed_temp = 7;break;
          case 30:  Display.Disp_Hex(Disp_NUM_1,8,Disp_DP_OFF); HMI.ucSpeed_temp = 8;break;
          case 20:  Display.Disp_Hex(Disp_NUM_1,9,Disp_DP_OFF); HMI.ucSpeed_temp = 9;break;
          default: break;
        }
        //往屏幕发送显示速度指令
        HMI.pucSend_Motor_Speed_Buffer[7] = HMI.ucSpeed_temp+'0';
        HMI.HMI_SendString(HMI.pucSend_Motor_Speed_Buffer);
      }
      else
      {
        HMI.HMI_SendString((uint8_t*)"n0.val=0");
        HMI.HMI_SendString((uint8_t*)"n1.val=0");
      }
    }
  }

  if(GPIO_Pin == KEY2_Pin)
  {
    //配网标志位置位
    ESP_12S.SmartConfig_Flag = TRUE;
    //长按2s配网
    for(i=0;i<100;i++)
    {
      HAL_Delay(10);
      //喂狗
      MyIWDG.Feed();
      //如果2s内按键松开，则退出循环，清除配网标志位
      if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin) == GPIO_PIN_SET)
      {
        ESP_12S.SmartConfig_Flag = FALSE;
        break;
      }
    }

    //如果不是配网，则调整步进电机方向
    if(ESP_12S.SmartConfig_Flag == FALSE)
    {
      //步进电机正反转
      Uniplar_Step_Motor.Direction_Adjust();
      HMI.pucSend_Motor_Cricle_Buffer[7] = Uniplar_Step_Motor.Circle+'0';  //圈数变量存入HMI发送数组中
      HMI.HMI_SendString(HMI.pucSend_Motor_Cricle_Buffer);   //发送HMI显示圈数指令
      if(HMI.Page_Step_Motor_KEY_Flag == FALSE)
      {
        HMI.HMI_SendString((uint8_t*)"click bt1,1");
        HMI.HMI_SendString((uint8_t*)"click bt1,0");
      }
    }
  }

  if(GPIO_Pin == KEY3_Pin)
  {
    //步进电机加速
    Uniplar_Step_Motor.Speed_Adjust(Speed_up);
    if(HMI.Page_Step_Motor_KEY_Flag == FALSE)
    {
      if(Uniplar_Step_Motor.Status == Start_Status)
      {
        HMI.HMI_SendString("click b3,1");
        HMI.HMI_SendString("click b3,0");
      }
    }
  }

  if(GPIO_Pin == KEY4_Pin)
  {
    //步进电机减速
    Uniplar_Step_Motor.Speed_Adjust(Speed_down);
    if(HMI.Page_Step_Motor_KEY_Flag == FALSE)
    {
      if(Uniplar_Step_Motor.Status == Start_Status)
      {
        HMI.HMI_SendString((uint8_t*)"click b4,1");
        HMI.HMI_SendString((uint8_t*)"click b4,0");
      }
    }
  }
}

/*
* @name   HAL_TIM_PeriodElapsedCallback
* @brief  定时器更新事件中断回调函数
* @param  htim：定时器
* @retval None   
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  //延时定时器
  if(htim->Instance == htim6.Instance)
  {
    Timer6.SHT30_Measure_Timeout++;
    Timer6.usDelay_Timer++;
    ESP_12S.TCP_Reconnect_Timer++;
    if(++Timer6.usMCU_Run_Timer > TIMER_1s)
    {
      Timer6.usMCU_Run_Timer = 0;
      LED.LED_Fun(LED1,LED_Flip);
      MyIWDG.Feed();    //喂狗
    }
  }

  //控制步进电机
  if(htim->Instance == htim7.Instance)
  {
    //没转完指定圈数
    if(Uniplar_Step_Motor.Circle > 0)
    {
      //电机转动一个脉冲
      Uniplar_Step_Motor.Step_One_Puls();
      //根据脉冲计数判断是否转了一圈
      if(++Uniplar_Step_Motor.Pulse_Cnt == Uniplar_Step_Motor.one_Circle_Pulse)
      {
        Uniplar_Step_Motor.Pulse_Cnt = 0;   //脉冲数清零，为下次准备
        Uniplar_Step_Motor.Circle--;        //已经转了1圈，圈数减1，启动时设置圈数为3
        Display.HMI_Or_SHT30_Control_Flag = Display_HMI;      //串口屏控制数码管
        HMI.pucSend_Motor_Cricle_Buffer[7] = Uniplar_Step_Motor.Circle+'0';  //圈数变量存入HMI发送数组中
        HMI.HMI_SendString(HMI.pucSend_Motor_Cricle_Buffer);   //发送HMI显示圈数指令
        Display.Disp_Hex(Disp_NUM_6,Uniplar_Step_Motor.Circle,Disp_DP_OFF); //显示圈数
        if(Uniplar_Step_Motor.Circle == 0)      //如果圈数转完了，则发一次点击屏幕开关按钮的指令，将按钮状态设为关
        {
          HMI.HMI_SendString((uint8_t*)"click bt0,1");
          HMI.HMI_SendString((uint8_t*)"click bt0,0");
        }
      }
    }
    else  //转完指定圈数
    {
      Uniplar_Step_Motor.Status = Stop_Status;  //电机状态为停止
      CLR_Motor_A;
      CLR_Motor_B;
      CLR_Motor_C;
      CLR_Motor_D;
      LED.LED_Fun(LED2,LED_OFF);    //关闭电机运行指示灯
    }
  }
}

/*
* @name   HAL_UART_TxCpltCallback
* @brief  串口发送完成中断回调函数
* @param  huart：串口指针
* @retval None   
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  /*当DMA将内存数据搬运到串口，待串口发送完后产生中断，
  经过多次函数调用，最终来到这里的中断回调函数*/
  if(huart->Instance == huart3.Instance)
  {
    //RS485设置为接收模式
    UART3.RS485_Set_RecMode();
  }
}

/*
* @name   HAL_UART_IDLECallback
* @brief  串口接收完成空闲中断回调函数(自己定义)
* @param  huart：串口指针
* @retval None   
*/ 
void HAL_UART_IDLECallback(UART_HandleTypeDef *huart)
{
  if(huart->Instance == huart1.Instance)
  {
    HMI.HMI_Protocol();
  }
}

/*
* @name   HAL_UARTEx_RxEventCallback
* @brief  串口接收完成空闲中断回调函数(HAL库)
* @param  huart：串口指针,Size：接收的数据大小
* @retval None   
*/ 
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
  //接收完成串口数据后，触发空闲中断，在这里处理接收数据
  if(huart->Instance == huart2.Instance)
  {
    ESP_12S.Receive_Information();
  }
  if(huart->Instance == huart3.Instance)
  {
    Modbus.Protocol_Analysis(&UART3);   //Modbus协议解析
    HAL_UARTEx_ReceiveToIdle_DMA(&huart3,UART3.pucRec_Buffer,PUCUART3_REC_BUFFER_LEN);   //继续接收数据
  }
}
/********************************************************
  End Of File
********************************************************/
